package com.code.app.util;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import com.code.app.service.DownLoaderService;
import com.code.app.R;
import com.code.app.activity.DownloaderActivity;
import com.code.app.entities.DownLoadInfo;

import java.util.HashMap;

public class NotificationUtil {

    private Context mContext;

    private NotificationManager mNotificationManager;

    private HashMap<Integer, Notification> notificationHashMap;

    public NotificationUtil(Context context) {
        this.mContext = context;
        this.mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        this.notificationHashMap = new HashMap<>();
    }

    public void showNotification(DownLoadInfo downLoadInfo) {
        Notification notification = new Notification();
        notification.tickerText = downLoadInfo.getDescroption();
        notification.when = System.currentTimeMillis();
        notification.icon = R.mipmap.ic_launcher;
        notification.flags = Notification.FLAG_AUTO_CANCEL;

        Intent intent = new Intent(mContext, DownloaderActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
        notification.contentIntent = pendingIntent;

        RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.layout_notification);

        remoteViews.setTextViewText(R.id.tv_content, downLoadInfo.getName());
        remoteViews.setProgressBar(R.id.pb_progress, 100, downLoadInfo.getProgress(), false);

        Intent intentStart = new Intent(mContext, DownLoaderService.class);
        intentStart.setAction(DownLoaderService.TASK_START);
        intentStart.putExtra("downLoadInfo", downLoadInfo);
        PendingIntent pendingIntentStart = PendingIntent.getService(mContext, 0, intentStart, 0);
        remoteViews.setOnClickPendingIntent(R.id.start, pendingIntentStart);

        Intent intentStop = new Intent(mContext, DownLoaderService.class);
        intentStop.setAction(DownLoaderService.TASK_STOP);
        intentStop.putExtra("downLoadInfo", downLoadInfo);
        PendingIntent pendingIntentStop = PendingIntent.getService(mContext, 0, intentStop, 0);
        remoteViews.setOnClickPendingIntent(R.id.stop, pendingIntentStop);

        notification.contentView = remoteViews;
        mNotificationManager.notify(downLoadInfo.getId(), notification);
        notificationHashMap.put(downLoadInfo.getId(), notification);
    }

    public void cancleNotification(int id) {
        mNotificationManager.cancel(id);
        notificationHashMap.remove(id);
    }

    public void updateProgress(int id, int progress) {
        Notification notification = notificationHashMap.get(id);
        if (notification != null) {
            notification.contentView.setProgressBar(R.id.pb_progress, 100, progress, false);
        }
    }

}
